/*
 * Copyright (c) 2020, RudyLo <luhuadong@163.com>
 *
 * SPDX-License-Identifier: MIT License
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-15     luhuadong    the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#include <string.h>
#include <stdio.h>
#include "ssd1306.h"
#include <sys/time.h>
#include "mcu_api.h"
#include "wifi.h"
#include "common.h"

static void display_temp_humi()
{
    char disp_buf[32];
    rt_sprintf(disp_buf,"Temp:%02d.%dC",s_m_current_data.temperature/10,s_m_current_data.temperature%10);
    ssd1306_SetCursor(0, 20);
    ssd1306_WriteString(disp_buf, Font_7x10, White);

    rt_sprintf(disp_buf,"Humi:%02d.%d%%",s_m_current_data.humidity/10,s_m_current_data.humidity%10);
    ssd1306_SetCursor(0, 20+12);
    ssd1306_WriteString(disp_buf, Font_7x10, White);
}

static void display_rtc_time()
{
    char disp_buf[32];

    time_t now;
    now = time(RT_NULL);

    rt_sprintf(disp_buf,"%.*s", 25, ctime(&now));
    ssd1306_SetCursor(SSD1306_WIDTH - 4*6,20);
    ssd1306_WriteString(disp_buf + 20, Font_6x8, White);

    rt_sprintf(disp_buf,"%.*s", 4, ctime(&now));
    ssd1306_SetCursor(SSD1306_WIDTH - 4*6,20 + 8);
    ssd1306_WriteString(disp_buf, Font_6x8, White);

    rt_sprintf(disp_buf,"%.*s", 10, ctime(&now));
    ssd1306_SetCursor(SSD1306_WIDTH - 6*6,20 + 8 * 2);
    ssd1306_WriteString(disp_buf + 4, Font_6x8, White);

    rt_sprintf(disp_buf,"%04d", s_m_current_data.illuminance);
    ssd1306_SetCursor(0,20 + 8 * 3 + 10);
    ssd1306_WriteString(disp_buf, Font_7x10, White);

    rt_sprintf(disp_buf,"%.*s", 20, ctime(&now));
    ssd1306_SetCursor(SSD1306_WIDTH - 8 * 11,SSD1306_HEIGHT- 18);
    ssd1306_WriteString(disp_buf + 11, Font_11x18, White);

}

bool is_rtc_time_not_sync = true;

static bool dispaly_voice_module_status()
{
    uint8_t wifi_status = mcu_get_wifi_work_state();
    

    if(wifi_status == WIFI_CONN_CLOUD)
    {
        if(is_rtc_time_not_sync == true)
        {
            mcu_get_system_time();
        }
    }
    
    return wifi_status == WIFI_CONN_CLOUD;
}

static void display_main_page()
{
    ssd1306_Fill(Black);
    
    ssd1306_SetCursor(0, 0);
    ssd1306_WriteString(" Smart Home", Font_11x18, White);
    
    display_temp_humi();
    
    if(dispaly_voice_module_status())
    {
        display_rtc_time();
    }

    ssd1306_UpdateScreen();
}

void display_process(void *avg)
{
    GoKit_RGB_led_Init();
    ssd1306_Init();

    while(true)
    {
        rt_thread_mdelay(1000);
        display_main_page();
    }
}


static int display_thread_create(void)
{
    rt_thread_t display_thread;

    display_thread = rt_thread_create("display_thread", display_process, 
                                    RT_NULL, 1024, 
                                    5, 20);
    
    if (display_thread) 
        return rt_thread_startup(display_thread);
    else 
        return RT_ERROR;
}

INIT_APP_EXPORT(display_thread_create);
